from db_init import db
import sqlalchemy as sa
from datetime import datetime


# 用户
class User(db.Model):
    id = sa.Column(sa.Integer, primary_key=True)
    login_time = sa.Column(sa.DateTime, default=datetime.now)
    name = sa.Column(sa.String(16))
    pwd = sa.Column(sa.String(50))
    phone = sa.Column(sa.String(11))
    img = sa.Column(sa.String(1024))


class House(db.Model):
    id = sa.Column(sa.Integer, primary_key=True)
    title = sa.Column(sa.String(16))
    info = sa.Column(sa.String(128))
    zheng = sa.Column(sa.Boolean)
    p_num = sa.Column(sa.Integer)
    c_num = sa.Column(sa.Integer)
    contract_id = db.Column(db.ForeignKey('contract.id'))
    master_id = db.Column(db.ForeignKey('user.id'))
    tenant_id = db.Column(db.ForeignKey('user.id'))
    live = sa.Column(sa.Boolean)


# 合同
class Contract(db.Model):
    id = sa.Column(sa.Integer, primary_key=True)
    price = sa.Column(sa.Integer)
    house_id = db.relationship("House", backref='contract')


house_facility = db.Table(
    'house_facility',
    db.Column('house_id', db.ForeignKey('house.id'), primary_key=True),
    db.Column('facility_id', db.ForeignKey('facility.id'), primary_key=True)

)


# 设施
class Facility(db.Model):
    id = sa.Column(sa.Integer, primary_key=True)
    name = sa.Column(sa.String(16))
    icon = sa.Column(sa.String(1024))


# 订单
class Order(db.Model):
    id = sa.Column(sa.Integer, primary_key=True)
    number_id = sa.Column(sa.Integer, unique=True)
    name = sa.Column(sa.String(16))
    house_id = db.Column(db.ForeignKey('house.id'))
    tenant_id = db.Column(db.ForeignKey('user.id'))
    start_t = db.Column(sa.Integer)
    end_t = db.Column(sa.Integer)


class Advertise(db.Model):
    id = sa.Column(sa.Integer, primary_key=True)
    title = sa.Column(sa.String(16))
    img = sa.Column(sa.String(1024))
    url = sa.Column(sa.String(1024))
    house_id = db.Column(db.ForeignKey('house.id'))


class Img(db.Model):
    id = sa.Column(sa.Integer, primary_key=True)
    title = sa.Column(sa.String(16))
    img = sa.Column(sa.String(1024))
    url = sa.Column(sa.String(1024))
    house_id = db.Column(db.ForeignKey('house.id'))

# 评论
class Comment(db.Model):
    id = sa.Column(sa.Integer, primary_key=True)
    content = sa.Column(sa.String(1024))
    house_id = db.Column(db.ForeignKey('house.id'))
    brau = db.Column(db.ForeignKey('comment.id'))
    reply = db.relationship('Comment',remote_side=[id],backref='beau_id')
    author = db.Column(db.ForeignKey('user.id'))


